package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.template.AliOssTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 通用controller
 */
@Api(tags = "通用接口")
@RestController
@RequestMapping("/admin/common")
public class CommonController {
    @Autowired
    AliOssTemplate aliOssTemplate;

    /**
     * 图片上传
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation("图片上传")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        //1.构造文件名称
        //文件的原始名称
        String fileRealName = file.getOriginalFilename();
        //截取文件后缀
        String suffix = fileRealName.substring(fileRealName.lastIndexOf("."));
        String finalFileName = UUID.randomUUID()+suffix;
        //2.上传阿里云,获取url
        String fileUrl = aliOssTemplate.upload(finalFileName,file.getInputStream());
        //3.返回前端
        return Result.success(fileUrl);
    }
}
